package com.cn.wanxi.util;

import com.cn.wanxi.dao.NavDao;

import javax.swing.plaf.PanelUI;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class IOTool {
    private static final String PATH = "E:\\wanxi\\step-3\\canyin03-1129\\web";
    private static final String DIRECTORY = "note";

    private static final String SPLIT = "\n";

    /**
     * 创建文件
     *
     * @return
     */
    public static File createFile(String fileName) {
        File dir = new File(PATH, DIRECTORY);
        dir.mkdirs();
        File file = new File(dir, fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static void saveData(File file, String[] data) {
        try {
            OutputStream outputStream = new FileOutputStream(file);
//            int length = NavDao.TITLES.length;
//            String[] data = {};
            for (int i = 0; i < data.length; i++) {

                outputStream.write(data[i].getBytes(StandardCharsets.UTF_8));
                outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String readData(File file) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStream inputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            Reader reader = new BufferedReader(inputStreamReader);
            int read = 0;
            while ((read = reader.read()) != -1) {
                stringBuilder.append((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
